package com.person.easyui;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class EasyUItools{
	
	public static List<TreeNode>getTreeNodeList(List<? extends Object> list,TreeNodeRule tnr){
		List<TreeNode> treeNodeList=new ArrayList<TreeNode>();
		if(list!=null&&list.size()>0){
				//对象为实体类
				for(Object o:list)
					treeNodeList.add(getTreeNodeByEntity(o,tnr));
		}
		return treeNodeList;
  }
	
	public static List<TreeNode>getTreeNodeListByMap(List<Map<String,Object>> list,TreeNodeRule tnr){
		List<TreeNode> treeNodeList=new ArrayList<TreeNode>();
		if(list!=null&&list.size()>0){
				//对象为Map
				for(Map<String,Object> o:list)
					treeNodeList.add(getTreeNodeByMap(o,tnr));
		}
		return treeNodeList;
  }
	
	private static TreeNode getTreeNodeByMap(Map<String,Object>map,TreeNodeRule tnr){
		if(map!=null){
			TreeNode tn=new TreeNode();
			Object id,parentId,text,iconCls,state,checked;
			if(tnr==null){
				id=map.get("id");
				parentId=map.get("parentId");
				text=map.get("text");
				iconCls=map.get("iconCls");
				state=map.get("state");
				checked=map.get("checked");
			}else{
				id=map.get(tnr.getId()==null?"id":tnr.getId());
				parentId=map.get(tnr.getParentId()==null?"parentId":tnr.getParentId());
				text=map.get(tnr.getText()==null?"text":tnr.getText());
				iconCls=map.get(tnr.getIconCls()==null?"iconCls":tnr.getIconCls());
				state=map.get(tnr.getState()==null?"state":tnr.getState());
				checked=map.get(tnr.getChecked()==null?"checked":tnr.getChecked());
			}
			tn.setId(id==null?null:String.valueOf(id));
			tn.setParentId(parentId==null?null:String.valueOf(parentId));
			tn.setText(text==null?null:String.valueOf(text));
			if(checked==null||String.valueOf(checked).equalsIgnoreCase("false"))
				tn.setChecked(null);
			else 
				tn.setChecked("true");
			tn.setIconCls(iconCls==null?null:String.valueOf(iconCls));
			tn.setState(state==null?null:String.valueOf(state));
            Iterator<String>itKey=map.keySet().iterator();
            Map<String,Object>attributes=tn.getAttributes();
    		if(attributes==null)attributes=new HashMap<String,Object>(); 
            while(itKey.hasNext()){
            	String key=itKey.next();
            	if(!"id".equals(key)&&!"parentId".equals(key)&&!"text".equals(key)
            			&&!"iconCls".equals(key)&&!"state".equals(key)&&!"checked".equals(key)){
            		attributes.put(key,map.get(key));
            	}
            }
    		tn.setAttributes(attributes);
			return tn;
		}
		return null;
	}
	
	 private static TreeNode getTreeNodeByEntity(Object object,TreeNodeRule tnr){
			if(object!=null){
				TreeNode tn=new TreeNode();
				Class c=object.getClass();
				Method[] ms=c.getMethods();
				Method m=null;
		     try {
		    	 Object id = null,parentId = null,text=null,iconCls=null,state=null,checked=null;
				if(tnr==null){
					    m=getMethod(ms,"id");
						id=m==null?null:m.invoke(object);
						m=getMethod(ms,"parentId");
						parentId=m==null?null:m.invoke(object);
						m=getMethod(ms,"text");
						text=m==null?null:m.invoke(object);
						m=getMethod(ms,"iconCls");
						iconCls=m==null?null:m.invoke(object);
						m=getMethod(ms,"state");
						state=getMethod(ms,"state").invoke(object);
						checked=m==null?null:m.invoke(object);
					}else{
						m=getMethod(ms,tnr.getId()==null?"id":tnr.getId());
						id=m==null?null:m.invoke(object);
						m=getMethod(ms,tnr.getParentId()==null?"parentId":tnr.getParentId());
						parentId=m==null?null:m.invoke(object);
						m=getMethod(ms,tnr.getText()==null?"text":tnr.getText());
						text=m==null?null:m.invoke(object);
						m=getMethod(ms,tnr.getIconCls()==null?"iconCls":tnr.getIconCls());
						iconCls=m==null?null:m.invoke(object);
						m=getMethod(ms,tnr.getState()==null?"state":tnr.getState());
						state=m==null?null:m.invoke(object);
						m=getMethod(ms,tnr.getChecked()==null?"checked":tnr.getChecked());
						checked=m==null?null:m.invoke(object);
					}
					tn.setId(id==null?null:String.valueOf(id));
					tn.setParentId(parentId==null?null:String.valueOf(parentId));
					tn.setText(text==null?null:String.valueOf(text));
					if(checked==null||String.valueOf(checked).equalsIgnoreCase("false"))
						tn.setChecked(null);
					else 
						tn.setChecked("true");
					tn.setIconCls(iconCls==null?null:String.valueOf(iconCls));
					tn.setState(state==null?null:String.valueOf(state));
		            Map<String,Object>attributes=tn.getAttributes();
		    		if(attributes==null)attributes=new HashMap<String,Object>(); 
		    		if(ms!=null){
		    			for(Method me:ms){
		    				if(me.getName().startsWith("get")){
		    					attributes.put(me.getName().substring(3,4).toLowerCase()+me.getName().substring(4),me.invoke(object));
		    				}
		    			}
		    		}
		    		tn.setAttributes(attributes);
		    		return tn;
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
			return null;
    }
	
	 private static Method getMethod(Method[]ms,String mName){
		 if(ms!=null){
               for(Method m:ms){
            	   if(m.getName().equalsIgnoreCase("get"+mName))
            		   return m;
               }			 
		 }
			 return null;
	 }
	 
	public static List<TreeNode> getUItreeList(List<TreeNode> treeDataList){
		if(treeDataList==null)return null;
        //找出所有父节点
		Map<String,TreeNode> topMap=new HashMap<String,TreeNode>();
		List<TreeNode> parentList=new ArrayList<TreeNode>();
		for(TreeNode tn:treeDataList)
			topMap.put(tn.getId(),tn);
		Iterator<TreeNode> ites = treeDataList.iterator();
	    while(ites.hasNext()){
	    	TreeNode tn=ites.next();
	    	if(StringUtils.isBlank(tn.getParentId())||!topMap.containsKey(tn.getParentId())){
	    		parentList.add(tn);
	    		ites.remove();
	    	}
	    }
	    for(TreeNode tn:parentList){
	    	guiTree(tn,treeDataList);
	    }
    	return parentList;
    }
	
	private static void guiTree(TreeNode parentNode,List<TreeNode> treeNodeList){
		if(treeNodeList==null)return;
		Iterator<TreeNode> ite=treeNodeList.iterator();
		while(ite.hasNext()){
			TreeNode tn=ite.next();
			if(parentNode.getId()!=null&&parentNode.getId().equals(tn.getParentId())){
				List<TreeNode>children = parentNode.getChildren();
				if(children==null)children=new ArrayList<TreeNode>();
				    children.add(tn);
				    parentNode.setChildren(children);
				    guiTree(tn,treeNodeList);
			}
		}
    }
}
    
